package cn.pegasus.teamwork.sysconfig.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.pegasus.teamwork.sysconfig.entity.SysMessagePush;
import cn.pegasus.teamwork.sysconfig.mapper.SysMessagePushMapper;
import cn.pegasus.teamwork.sysconfig.service.SysMessagePushService;

@Service
public class SysMessagePushServiceImpl extends BaseServiceImpl<SysMessagePush, SysMessagePushMapper> implements SysMessagePushService{

	// 注入当前dao对象
    @Autowired
    private SysMessagePushMapper sysMessagePushMapper;
    
    /**
     * 添加推送用户
     */
	public int addSysMessagePush(SysMessagePush sysMessagePush) throws Exception {
		try {
			sysMessagePushMapper.insertByEntity(sysMessagePush);
			return 0;
		} catch (Exception e) {
			logger.error("添加消息失败",e);
			return -1;
		}
		
	}
	
	/**
	 * 查询推送用户
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public SysMessagePush getSysMessagePush(SysMessagePush sysMessagePush) throws Exception {
		return sysMessagePushMapper.selectSingle(sysMessagePush);
	}
	
	
	/**
	 * 查询所有推送用户
	 * @return
	 * @throws Exception
	 */
	public List<SysMessagePush> getSysMessagePushList()throws Exception {
		return sysMessagePushMapper.selectByObject(new SysMessagePush());
	}
	
	
	
	/**
	 * 判断用户是否存在推送记录
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public boolean checkSysMessagePush(Long userId)throws Exception {
		boolean flag = false;
		SysMessagePush sysMessagePush = new SysMessagePush();
		sysMessagePush.setUserId(userId);
		sysMessagePush = sysMessagePushMapper.selectSingle(sysMessagePush);
		if(sysMessagePush!=null){
			flag = true;
		}
		return flag;
	}
	
	
	
	/**
	 * 修改推送用户
	 * @param id
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public int updateSysMessagePush(SysMessagePush sysMessagePush) throws Exception {
		try {
			if(sysMessagePush.getUserId()!=null){
				sysMessagePushMapper.updateByEntity(sysMessagePush);
			}else{
				logger.error("推送用户userId缺失");
				return -2;
			}
			return 0;
		} catch (Exception e) {
			logger.error("修改系统推送消息失败",e);
			return -1;
		}
		
	}
	
}
